package com.egao.common.module.credits.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.core.web.PageParam;
import com.egao.common.module.credits.entity.Credits;
import com.egao.common.module.credits.mapper.CreditsMapper;
import com.egao.common.module.credits.service.CreditsService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lym
 * @date 2022/1/20 13:44
 */
@Service
public class CreditsServiceImpl extends ServiceImpl<CreditsMapper, Credits> implements CreditsService {

    @Override
    public Credits get(String type, Integer businessId) {
        PageParam<Credits> page = new PageParam<>();
        page.put("type", type);
        page.put("businessId", businessId);
        List<Credits> list = listPage(page);
        if (list == null || list.size() == 0) {
            return null;
        }

        return list.get(0);
    }

    @Override
    public void saveCredits(Credits credits) {
        if (credits.getCredits() == null) {
            return;
        }

        baseMapper.insert(credits);
    }

    @Override
    public void updateCredits(Credits credits) {
        Credits old = get(credits.getType(), credits.getBusinessId());
        if (old == null) {
            saveCredits(credits);
            return;
        }

        credits.setId(old.getId());
        baseMapper.updateById(credits);
    }

    @Override
    public void deletedCredits(Credits credits) {
        Credits old = get(credits.getType(), credits.getBusinessId());
        if (old == null) {
            return;
        }

        baseMapper.deleteById(old);
    }

    @Override
    public List<Credits> listPage(PageParam<Credits> page) {
        page.setDefaultOrder(null, new String[]{"create_time"});
        return baseMapper.listPage(page);
    }
}
